/**************************************************************************
 *
 *
 *					此文件为自动生成 不要自行更改!!!
 *
 *
 *************************************************************************/
using System;
using System.Reflection;
using System.Text;
using System.IO;
using ProtoBuf;
using serverdata;
using System.Data.OleDb;
using System.Data;

namespace BinDataTool
{
    class ServerParser
    {
        // const字符串
        public static string strEnd = " \r\n";
        public static string strXlsx = ".xlsx";
        public static string strDat = ".bytes";
        public static string NowTime() { return DateTime.Now.ToString() + " "; }

        // Excel相关对象
        public static OleDbConnection gConnection;
        public static DataSet gSheetData;
        public static int nSheetIndex;

        // 打开工作薄
        public static void GetBook(string szName)
        {
            string source = "Provider = Microsoft.Ace.OLEDB.12.0; Data Source = '" + szName + "'; Extended Properties = 'Excel 12.0;HDR=NO;IMEX=1'";
            gConnection = new OleDbConnection(source);
        }

        // 打开指定Worksheet
        public static bool GetSheet()
        {
            string select = "SELECT * FROM [sheet1$]";
            OleDbDataAdapter readCommand = new OleDbDataAdapter(select, gConnection);
            gSheetData = new DataSet();
            readCommand.Fill(gSheetData);
            return true;
        }

        // 有效行数
        private static int _realRows = 0;
        // 行数
        public static int Rows()
        { return _realRows <= 0 ? gSheetData.Tables[0].Rows.Count : _realRows; }

        // 有效列数
        private static int _realCols = 0;
        // 列数
        public static int Cols()
        { return _realCols <= 0 ? gSheetData.Tables[0].Columns.Count : _realCols; }

        // 分割字符串
        public static String[] SubString(String strData, char separator)
        {
            String[] strArray = strData.Split(separator);
            return strArray;
        }

        public static string GetString(int i, int j)
        {
            if (null == gSheetData.Tables[0].Rows)
            { return ""; }
            return gSheetData.Tables[0].Rows[i][j].ToString().Replace("\n", "");
        }

        // 解析整型字段
        public static Int32 ReadInt32(int i, int j)
        {
            if (null == gSheetData.Tables[0].Rows)
            { return 0; }
            string str = gSheetData.Tables[0].Rows[i][j].ToString().Replace("\n", "");
            if (str == "")
                return 0;
            return System.Convert.ToInt32(str);
        }

        // 解析Bool字段
        public static bool ReadBool(int i, int j)
        {
            if (null == gSheetData.Tables[0].Rows)
            { return false; }
            string str = gSheetData.Tables[0].Rows[i][j].ToString().Replace("\n", "");
            if (str == "")
                return false;
            string stu = str.Trim().ToUpper();
            if (stu.Equals("FALSE") || stu.Equals("0") || stu.Equals("假")) { return false; }
            return true;
        }

        public static Int64 ReadInt64(int i, int j)
        {
            if (null == gSheetData.Tables[0].Rows)
            { return 0; }
            string str = gSheetData.Tables[0].Rows[i][j].ToString().Replace("\n", "");
            if (str == "")
                return 0;
            return System.Convert.ToInt64(str);
        }

        // 解析float
        public static float ReadFloat(int i, int j)
        {
            if (null == gSheetData.Tables[0].Rows)
            { return 0; }
            string str = gSheetData.Tables[0].Rows[i][j].ToString().Replace("\n", "");
            if (str == "")
                return 0;
            return System.Convert.ToSingle(str);
        }

        // 解析字符串
        public static byte[] ReadString(int i, int j)
        {
            object element = gSheetData.Tables[0].Rows[i][j];
            if (null == element)
            { return Encoding.UTF8.GetBytes(""); }
            return Encoding.UTF8.GetBytes(element.ToString().Replace("\n", ""));
        }

        // 开始解析Excel
        private static void StartParse(string path)
        {
            nSheetIndex = 1;
            GetBook(MeFile.GetFilePath(path));
            GetSheet();
            //init real rows
            for (int i = 0; i < gSheetData.Tables[0].Rows.Count; i++)
            {
                if (gSheetData.Tables[0].Rows[i][0].ToString().Trim().Equals("__END__"))
                {
                    _realRows = i;
                    break;
                }
            }
            //init real columns
            for (int j = 0; j < gSheetData.Tables[0].Columns.Count; j++)
            {
                if (gSheetData.Tables[0].Rows[0][j].ToString().Trim().Equals("__END__"))
                {
                    _realCols = j;
                    break;
                }
            }
        }

        // 结束解析Excel
        public static void EndParse()
        {
            nSheetIndex = 1;
            gConnection = null;
            gSheetData = null;
            _realRows = 0;
            _realCols = 0;
        }

        public static void ParseServerHero(string fileName)
        {
            StartParse(fileName);
            serverdata.HeroConfig xList = new serverdata.HeroConfig();
            int x = 0;
            string[] sArray;
            // 数据从第5行开始
            for (int i = 4; i < Rows(); ++i)
            {
                serverdata.HeroData xData = new serverdata.HeroData();
                xData.id = ReadInt32(i, x++);
                x++;
                x = 0;
                xList.data.Add(xData);
            }

            string fPath = System.Environment.CurrentDirectory + @"\dat_server\";
            FileStream wFile = new FileStream(fPath + fileName + ".bytes", FileMode.Create, FileAccess.Write);
            Serializer.Serialize<HeroConfig>(wFile, xList);
            wFile.Close();

            FileStream rFile = new FileStream(fPath + fileName + ".bytes", FileMode.Open, FileAccess.Read);
            HeroConfig readList = Serializer.Deserialize<HeroConfig>(rFile);
            rFile.Close();
            EndParse();
        }

        public static void ParseServerSqlScript(string fileName)
        {
            StartParse(fileName);
            serverdata.SqlScriptConfig xList = new serverdata.SqlScriptConfig();
            int x = 0;
            string[] sArray;
            // 数据从第5行开始
            for (int i = 4; i < Rows(); ++i)
            {
                serverdata.SqlScriptData xData = new serverdata.SqlScriptData();
                xData.id = ReadInt32(i, x++);
                xData.sql = ReadString(i, x++);
                x = 0;
                xList.data.Add(xData);
            }

            string fPath = System.Environment.CurrentDirectory + @"\dat_server\";
            FileStream wFile = new FileStream(fPath + fileName + ".bytes", FileMode.Create, FileAccess.Write);
            Serializer.Serialize<SqlScriptConfig>(wFile, xList);
            wFile.Close();

            FileStream rFile = new FileStream(fPath + fileName + ".bytes", FileMode.Open, FileAccess.Read);
            SqlScriptConfig readList = Serializer.Deserialize<SqlScriptConfig>(rFile);
            rFile.Close();
            EndParse();
        }

        public static void ParseServer(string fileName)
        {
            if (fileName.Contains("$"))
            { return; }
            switch (fileName)
            {
                case "Hero":
                    { ParseServerHero(fileName); }
                    break;
                case "SqlScript":
                    { ParseServerSqlScript(fileName); }
                    break;
                default:
                    break;
            }
        }
    }
}
